<?php


use Laravel\CLI\Tasks\Bundle\Publisher;

class Admin_User_Controller extends Base_Controller
{

	public $restful = true;


	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("admin");

	}


	public function get_index()
	{
			
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			$this->theme->title("::User Management::");
			$data['userss'] = DB::table('users')->paginate(10, array('id', 'name','status'));
		}


		//$data['role'] = $role->name;





		return $this->theme->render('admin::user.index', $data);
	}

	public function get_create()
	{
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			$this->theme->title("::User Management::");

			$data['MODULE'] = 'USER MANAGEMENT';
			$data['TITLE'] = 'User Creation';
			$data['DESCRIPTION'] = 'Create Users with Role';


			$data['users'] = DB::table('users')->get();
			$data['roles'] = DB::table('roles')->get();
			return $this->theme->render('admin::user.create', $data);
		}
	}

	public function post_create()
	{
		$rules = array(
				'name' => 'required',
				'userid' => 'required|between:4,50',
				'email' => 'required|unique:users|email',
				'role_id' => 'required',
				'password'         => 'required',
				'password_confirm' => 'required|same:password',
				'user_status' => 'required'
		);

		$v = Validator::make(Input::all(), $rules);

		if( $v->fails() )
		{
			// code for validation failure :(
			return Redirect::back()
			->with_errors($v)
			->with_input();
		}
		else
		{
			// code for validation success!
			/*   $user = new Users;
			$user->name     = Input::get('name');
			$user->username     = Input::get('userid');
			$user->role_id     = Input::get('role_id');
			$user->email    = Input::get('email');
			$user->password = Hash::make(Input::get('password'));

			$user->save();
			*/

			DB::table('users')->insert(array(

					'name' => Input::get('name'),
					'username' => Input::get('userid'),
					'role_id' => Input::get('role_id'),
					'email' => Input::get('email'),
					'password' => Hash::make(Input::get('password')),
					'status' => Input::get('user_status')
			));

			$msg = 'Successfully Added....';
			return Redirect::back()->with('message',$msg);
		}



	}

	public function get_edit($id){
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			$data['user']['info'] = DB::table('users')->where('id','=',$id)->first();



			$data['MODULE'] = 'USER MANAGEMENT';
			$data['TITLE'] = 'Edit User';
			$data['DESCRIPTION'] = 'Edit Users with Role';
			$data['roles'] = DB::table('roles')->get();

			return $this->theme->render('admin::user.edit', $data);
		}
			
	}

	public function post_edit(){
			
			
			
		$rules = array(
				'name' => 'required',
				'userid' => 'required|between:4,50',
				'email' => 'required|email',
				'role_id' => 'required',
				'password'         => 'required',
				'password_confirm' => 'required|same:password',
				'user_status' => 'required'
		);
			
			
		if(Input::get('password') == TRUE){
			$v = Validator::make(Input::all(), $rules);

			$thisOrderid = Input::get('id');


			if( $v->fails() )
			{


				// code for validation failure :(
				return Redirect::back()
				->with_errors($v)
				->with_input();
			}
			else
			{


				DB::table('users')->where('id','=',$thisOrderid)->update(array(
							
						'name' => Input::get('name'),
						'username' => Input::get('userid'),
						'role_id' => Input::get('role_id'),
						'email' => Input::get('email'),
						'password' => Hash::make(Input::get('password')),
						'status' => Input::get('user_status')
				));
					
			}
		}
		else{
			$thisOrderid = Input::get('id');

			DB::table('users')->where('id','=',$thisOrderid)->update(array(

					'name' => Input::get('name'),
					'username' => Input::get('userid'),
					'role_id' => Input::get('role_id'),
					'email' => Input::get('email'),
					'status' => Input::get('user_status')
			));


			$msg = '<div class="alert alert-info">Successfully Added....</div>';
			return Redirect::back()->with('message',$msg);
		}
	}

	public function get_update_status($id,$status){
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			if($status == '51'){

				$status = '1';
				$users =DB::table('users')
				->where('id', '=', $id)
				->update(array(

						'status' => $status,

				)
				);



			}else{
				$status = '51';
				$users =DB::table('users')
				->where('id', '=', $id)
				->update(array(

						'status' => $status,

				)
				);


			}

			return Redirect::to("admin/user");
		}
	}


	public function get_delete($id){
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			DB::table('users')->where('id', '=', $id)->delete();

			return Redirect::to("admin/user");
		}
	}


	public function get_facebook(){

		$data['fn'] = Config::get('Member::facebook.app_id');
			
		$data['secret'] = Config::get('Member::facebook.secret');
			
		$this->theme->title("Facebook Login Auth");
		return $this->theme->render('admin::user.facebook',$data);
			
	}

	public function post_facebook(){
			
		$filename = 'facebook.php';

		$dirPath = path('bundle').'member/config/';
			
			
			
		$fn = $dirPath . $filename;

			
			
		if (file_exists($fn)) {

			if (Input::get('app_id'))
			{


				$rules = array(
							
						'app_id' => 'required',
						'secret' => 'required'
				);
					
				$v = Validator::make(Input::all(), $rules);
				if( $v->fails() )
				{
					$msg = '<div class="alert alert-warning">Invalid Input....</div>';
					return Redirect::back()->with('message',$msg);
				}else{

					$app_id = stripslashes(Input::get('app_id'));
					//dd($content);

					$secret = Input::get('secret');
					$app_id = "<?php return array(

					'app_id' => '$app_id',
					'secret' => '$secret',
					);";

					$fp = fopen($fn,"w") or die ("Error opening file in write mode!");


					fputs($fp,$app_id);
					fclose($fp) or die ("Error closing file!");

					$msg = '<div class="alert alert-info">Successfully Added....</div>';
					return Redirect::back()->with('message',$msg);
				}
					
			}
			}else{

				$filename = 'facebook.php';
				$directory = path('bundle').'member/config/';
				$fn = $directory . $filename;




				if( file_exists( $filename ) ){

					dd("EXISTS");

				}else{

					$rules = array(

							'app_id' => 'required',
							'secret' => 'required'
					);

					$v = Validator::make(Input::all(), $rules);
					if( $v->fails() )
					{


						$msg = '<div class="alert alert-warning">Invalid Input....</div>';
						return Redirect::back()->with('message',$msg);
					}else{
						//dd($content);

						$secret = Input::get('secret');
						$app_id = Input::get('app_id');

						$fdata = "<?php return array(

						'app_id' => '$app_id',
						'secret' => '$secret',
						);";


						file_put_contents( $fn , $fdata );

						$msg = '<div class="alert alert-info">Successfully Added....</div>';
						return Redirect::back()->with('message',$msg);
					}
				}
			}








			/*$rules = array(

					'app_id' => 'required',
					'secret_code' => 'required'
			);


			$v = Validator::make(Input::all(), $rules);

			if( $v->fails() )
			{
			// code for validation failure :(
					$msg = '<div class="alert alert-warning">Invalid Input....</div>';
					return Redirect::back()->with('message',$msg);
					}
					else
			{

					$post_data = array(

							'app_id' => Input::get('app_id'),
							'secret' => Input::get('secret_code')
					);

					DB::table('facebook')->update($post_data);

					$msg = '<div class="alert alert-info">Successfully Added....</div>';
					return Redirect::back()->with('message',$msg);
					}*/
		}
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	